/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */

package com.gadglet.data;

import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.gadglet.data.utils.PMF;
import com.google.appengine.api.NamespaceManager;

/**
 * Utility class for Gadgets
 * 
 * this class must be use while disabling NamespaceManager
 *
 */
public class GadgetUtils {
	static Logger log = Logger.getLogger("GadgetUtils");
	
	public static Gadget getGadget(String name){
		Gadget gadget = null;

		String  nameSpace = NamespaceManager.get();
		NamespaceManager.set("");
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Gadget.class);
	
		try {
		
			query.setFilter("name == nameParam ");
			query.declareParameters("String nameParam");
			query.setUnique(true);
			gadget = (Gadget) query.execute(name);

		} catch (Exception e) {
			log.warning(e.getMessage());
		} finally {
	
			pm.close();
			NamespaceManager.set(nameSpace);

		}
	
	NamespaceManager.set(nameSpace);
	return gadget;
		
	}
}
